package com.heima.Serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.Config.ThreadUtil;
import com.heima.Mapper.WemediaUserMapper;
import com.heima.Mapper.WemediamaterialMapper;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.MaterialPagereuestDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.UUID;

@Service
public class WemediamaterialService implements com.heima.Serivce.WemediamaterialService {
    @Autowired
    WemediamaterialMapper wemediamaterialMapper;

    @Autowired
    WemediaUserMapper wemediaUserMapper;
    @Autowired
    FileStorageService fileStorageService;

    //    上传图片
    @Override
    public ResponseResult UpLoadImg(MultipartFile multipartFile) {
        WmMaterial wmMaterial = new WmMaterial();
        try {
//            获取传过来的图片
            String Filename=UUID.randomUUID().toString().replace("-", "");

//            上传到minio
            String url = fileStorageService.uploadImgFile("",
                    Filename+".jpg",
                    multipartFile.getInputStream());
            System.out.println(url);
            wmMaterial.setUrl(url);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if(wmMaterial.getUrl()==null)
        {
            return ResponseResult.errorResult(504,"图片路径为空");
        }
//        获取线程里的数据
        Integer id=ThreadUtil.threadLocal.get().getId();
        System.out.println(id);
        wmMaterial.setUserId(ThreadUtil.threadLocal.get().getId());
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 1);
        wmMaterial.setCreatedTime(new Date());
        wemediamaterialMapper.insert(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

//    查询素材
    @Override
    public ResponseResult GetMaterialList(MaterialPagereuestDto materialPagereuestDto) {

        if(materialPagereuestDto==null)
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        Integer UserId=ThreadUtil.threadLocal.get().getId();
//        判断用户是否存在
        WmUser wmUser = wemediaUserMapper.selectById(UserId);
        if(wmUser==null)
            return ResponseResult.okResult(null);

//        查询素材
        Page  page=new Page(materialPagereuestDto.getPage(),materialPagereuestDto.getSize());
        QueryWrapper<WmMaterial> wrapper=new QueryWrapper<>();
        Integer IsCollection=materialPagereuestDto.getIsCollection();
//        判断是查询全部还是查询收藏
        if(IsCollection==(short)1)
            wrapper.eq("is_collection",IsCollection);

        wrapper.eq("user_id",UserId).orderByDesc("created_time");
        Page returnpage = wemediamaterialMapper.selectPage(page, wrapper);
        return ResponseResult.okResult(returnpage.getRecords());
    }

//    删除素材
    @Override
    public ResponseResult DelMaterial(Integer id) {
        Integer UserId=ThreadUtil.threadLocal.get().getId();
//        判断用户是否一致
        WmMaterial wmMaterial = wemediamaterialMapper.selectById(id);
        if(wmMaterial.getUserId().equals(UserId)==false)
            return ResponseResult.errorResult(403,"权限不够");
//        删除minio的素材
        fileStorageService.delete(wmMaterial.getUrl());
//        删除数据库信息
        int num = wemediamaterialMapper.deleteById(id);
        return ResponseResult.okResult(num);
    }

//       取消素材收藏
    @Override
    public ResponseResult CancelCollect(Integer id ,Short type) {
//        判断素材是否存在
        WmMaterial wmMaterial = wemediamaterialMapper.selectById(id);
        Integer UserId = ThreadUtil.threadLocal.get().getId();
        if(UserId.equals(wmMaterial.getUserId())==false)
            return ResponseResult.errorResult(403,"权限不够");

        QueryWrapper<WmMaterial> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id).eq("url",wmMaterial.getUrl());
        wmMaterial.setIsCollection(type);
        int update = wemediamaterialMapper.update(wmMaterial, wrapper);
        return ResponseResult.okResult(update);
    }



}
